package com.zf.generator.templates;

import java.util.List;

import com.zf.generator.bean.*;

public class JspInputTemplate {
	protected static String nl;

	public static synchronized JspInputTemplate create(String lineSeparator) {
		nl = lineSeparator;
		JspInputTemplate result = new JspInputTemplate();
		nl = null;
		return result;
	}

	public final String NL = nl == null ? (System.getProperties()
			.getProperty("line.separator")) : nl;
	protected final String TEXT_1 = "<%@ page contentType=\"text/html; charset=UTF-8\" pageEncoding=\"UTF-8\"%>"
			+ NL
			+ "<%@ include file=\"/WEB-INF/jsp/commons/taglibs.jsp\"%>"
			+ NL
			+ "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
			+ NL + "<html>" + NL + "<head>" + NL + "<title>";
	protected final String TEXT_2 = "表单</title>" + NL
			+ "<%@ include file=\"/WEB-INF/jsp/commons/meta.jsp\"%>" + NL
			+ "<script>" + NL + "\t$(document).ready(function(){" + NL
			+ "\t\t//聚焦第一个输入框" + NL + "\t\t$(\"#name\").focus();" + NL
			+ "\t\t//为inputForm注册validate函数" + NL
			+ "\t\t$(\"#inputForm\").validate({" + NL + "\t\t\trules: {" + NL
			+ "\t\t\t\t";
	protected final String TEXT_3 = "\t" + NL + "\t\t\t}" + NL + "\t\t});" + NL
			+ "\t});" + NL + "\t</script>" + NL + "</head>" + NL + "<body>"
			+ NL + "<div id=\"container\" class=\"container_24\">" + NL
			+ "  <!-- header -->" + NL + "  <div id=\"header\">" + NL
			+ "    <%@ include file=\"/WEB-INF/jsp/commons/header.jsp\"%>" + NL
			+ "  </div>" + NL + "  " + NL + "  <!-- content -->" + NL
			+ "  <div id=\"content\" class=\"container_24\">" + NL
			+ "    <div class=\" suffix_5 grid_14 prefix_5\">" + NL
			+ "    \t<div class=\"area\">" + NL
			+ "    \t\t<form id=\"inputForm\" action=\"${ctx}/";
	protected final String TEXT_4 = "/";
	protected final String TEXT_5 = "/save\" method=\"post\">" + NL
			+ "\t\t      <input type=\"hidden\" name=\"id\" value=\"${";
	protected final String TEXT_6 = ".id}\" />" + NL + "\t\t      <table>" + NL
			+ "\t\t      \t";
	protected final String TEXT_7 = NL + "\t\t        <tr>" + NL
			+ "\t\t          <td>";
	protected final String TEXT_8 = ":</td>" + NL
			+ "\t\t          <td><input id=\"";
	protected final String TEXT_9 = "\" name=\"";
	protected final String TEXT_10 = "\" type=\"text\" value=\"${";
	protected final String TEXT_11 = ".";
	protected final String TEXT_12 = "}\" size=\"30\" maxlength=\"80\" /></td>"
			+ NL + "\t\t        </tr>" + NL + "\t\t        ";
	protected final String TEXT_13 = NL
			+ "\t\t        <tr>"
			+ NL
			+ "\t\t          <td colspan=\"2\"><input class=\"submit\" type=\"submit\" value=\"提交\" />"
			+ NL
			+ "\t\t            &nbsp;"
			+ NL
			+ "\t\t            <input class=\"submit\" type=\"button\" value=\"返回\" onClick=\"history.back()\" /></td>"
			+ NL + "\t\t        </tr>" + NL + "\t\t      </table>" + NL
			+ "\t\t    </form>" + NL + "    \t</div>" + NL + "    \t" + NL
			+ "    </div>" + NL + "  </div>  " + NL + "  " + NL
			+ "  <!-- footer -->" + NL + "  <div id=\"footer\">" + NL
			+ "    <%@ include file=\"/WEB-INF/jsp/commons/footer.jsp\"%>" + NL
			+ "  </div>" + NL + "</div>" + NL + "</body>" + NL + "</html>";
	protected final String TEXT_14 = NL;

	public String generate(Object argument) {
		final StringBuffer stringBuffer = new StringBuffer();

		JspTemplateArgs args = (JspTemplateArgs) argument;

		String modeName = args.getModeName();
		String entityName = args.getEntityName();
		String jspName = args.getJspName();

		List<EntityFieldInfo> entityFieldInfoList = args
				.getEntityFieldInfoList();

		String entityPackageName = args.getPackageNameInfo()
				.getEntityPackageName();
		String daoPackageName = args.getPackageNameInfo().getDaoPackageName();
		String servicePackageName = args.getPackageNameInfo()
				.getServicPackageName();
		String actionPackageName = args.getPackageNameInfo()
				.getActionPackageName();

		String entityClassName = args.getClassNameInfo().getEntityClassName();
		String daoClassName = args.getClassNameInfo().getDaoClassName();
		String serviceClassName = args.getClassNameInfo().getServicClassName();
		String actionClassName = args.getClassNameInfo().getActionClassName();

		String entityObjectName = args.getClassNameInfo().getEntityObjectName();
		String daoObjectName = args.getClassNameInfo().getDaoObjectName();
		String serviceObjectName = args.getClassNameInfo()
				.getServicObjectName();
		String actionObjectName = args.getClassNameInfo().getActionObjectName();
		String jsCode = "";
		StringBuffer temp = new StringBuffer();
		for (EntityFieldInfo entityFieldInfo : entityFieldInfoList) {
			List<String> fieldNames = entityFieldInfo.getFieldNames();

			for (String fieldName : fieldNames) {
				temp.append(fieldName + ":\"required\",");
			}
		}
		jsCode = temp.substring(0, temp.length() - 1);

		stringBuffer.append(TEXT_1);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_2);
		stringBuffer.append(jsCode);
		stringBuffer.append(TEXT_3);
		stringBuffer.append(modeName);
		stringBuffer.append(TEXT_4);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_5);
		stringBuffer.append(entityObjectName);
		stringBuffer.append(TEXT_6);

		for (EntityFieldInfo entityFieldInfo : entityFieldInfoList) {
			List<String> fieldNames = entityFieldInfo.getFieldNames();
			for (String fieldName : fieldNames) {

				stringBuffer.append(TEXT_7);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_8);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_9);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_10);
				stringBuffer.append(entityObjectName);
				stringBuffer.append(TEXT_11);
				stringBuffer.append(fieldName);
				stringBuffer.append(TEXT_12);

			}
		}

		stringBuffer.append(TEXT_13);
		stringBuffer.append(TEXT_14);
		return stringBuffer.toString();
	}
}
